home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / mail / pathalia.zoo / src / getopt.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  3KB  |  119 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. /*
  4.  *----------------------------------------------------------------------------
  5.  *
  6.  *    This getopt is derived from one posted to the net as part of
  7.  *    the smail 2.5 distribution.  I have had to patch it to allow
  8.  *    for a few MWC 2.0 and/or Atari TOS peculiarities.  These patches
  9.  *    are noted in comments marked `:rwa:'.  The orginal header follows.
  10.  *
  11.  *----------------------------------------------------------------------------
  12.  */
  13.  
  14. /*
  15. **    @(#)getopt.c    2.5 (smail) 9/15/87
  16. */
  17.  
  18. /*
  19.  * Here's something you've all been waiting for:  the AT&T public domain
  20.  * source for getopt(3).  It is the code which was given out at the 1985
  21.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  22.  * directly from AT&T.  The people there assure me that it is indeed
  23.  * in the public domain.
  24.  * 
  25.  * There is no manual page.  That is because the one they gave out at
  26.  * UNIFORUM was slightly different from the current System V Release 2
  27.  * manual page.  The difference apparently involved a note about the
  28.  * famous rules 5 and 6, recommending using white space between an option
  29.  * and its first argument, and not grouping options that have arguments.
  30.  * Getopt itself is currently lenient about both of these things White
  31.  * space is allowed, but not mandatory, and the last option in a group can
  32.  * have an argument.  That particular version of the man page evidently
  33.  * has no official existence, and my source at AT&T did not send a copy.
  34.  * The current SVR2 man page reflects the actual behavor of this getopt.
  35.  * However, I am not about to post a copy of anything licensed by AT&T.
  36.  */
  37.  
  38. /*LINTLIBRARY*/
  39. /* #define NULL    0 */
  40. /* #define EOF    (-1) */
  41.  
  42. /*    MWC can't hack the original macro (too complex). I have made it
  43.     simpler and defined a function, prterr(), to do the work. :rwa:        */
  44.  
  45. #define ERR(s, c)    if ( opterr ) prterr( argv, s, c )
  46.  
  47. static void
  48. prterr( argv, s, c )
  49.     char * * argv;
  50.     char * s;
  51.     int c; {
  52.     extern int strlen();
  53.     char errbuf[3];
  54.  
  55.     errbuf[0] = c;
  56.     errbuf[1] = '\r';        /* added \r to pander to TOS :rwa: */
  57.     errbuf[2] = '\n';
  58.  
  59.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));
  60.     (void) write(2, s, (unsigned)strlen(s));
  61.     (void) write(2, errbuf, 3);
  62. }
  63.  
  64. #define strchr index            /* SysV-ism, use v7 routine :rwa: */
  65.  
  66. extern int strcmp();
  67. extern char *strchr();
  68.  
  69. int    opterr = 1;
  70. int    optind = 1;
  71. int    optopt;
  72. char    *optarg;
  73.  
  74. int
  75. getopt(argc, argv, opts)
  76. int    argc;
  77. char    **argv, *opts;
  78. {
  79.     static int sp = 1;
  80.     register int c;
  81.     register char *cp;
  82.  
  83.     if(sp == 1)
  84.         if(optind >= argc ||
  85.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  86.             return(EOF);
  87.         else if(strcmp(argv[optind], "--") == NULL) {
  88.             optind++;
  89.             return(EOF);
  90.         }
  91.     optopt = c = argv[optind][sp];
  92.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  93.         ERR(": unknown option, -", c);
  94.         if(argv[optind][++sp] == '\0') {
  95.             optind++;
  96.             sp = 1;
  97.         }
  98.         return('?');
  99.     }
  100.     if(*++cp == ':') {
  101.         if(argv[optind][sp+1] != '\0')
  102.             optarg = &argv[optind++][sp+1];
  103.         else if(++optind >= argc) {
  104.             ERR(": argument missing for -", c);
  105.             sp = 1;
  106.             return('?');
  107.         } else
  108.             optarg = argv[optind++];
  109.         sp = 1;
  110.     } else {
  111.         if(argv[optind][++sp] == '\0') {
  112.             sp = 1;
  113.             optind++;
  114.         }
  115.         optarg = NULL;
  116.     }
  117.     return(c);
  118. }
  119.